package yunhe.entity;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Point;
import java.util.LinkedList;

import yunhe.listener.SnakeListener;
import yunhe.util.Global;

/**
 * 蛇
 * @author Administrator
 *
 */
public class Snake {
	private SnakeListener snakeListener;
	private LinkedList<Point> body=new LinkedList<Point>();
	public static final int UP=1;
	public static final int DOWN=2;
	public static final int LEFT=3;
	public static final int RIGHT=4;
	boolean life=true;
	private Point weiba;//存储蛇的尾巴
	//定义初始方向direction
	private int direction;
	public Snake(){
		init();
	}
	private void init(){
		int x=Global.HEIGHT/2;
		int y=Global.WIDTH/2;
		for(int i=0;i<3;i++){
			body.add(new Point(x-i,y));
		}
		//初始方向赋值为RIGHT(向右)
		this.direction=RIGHT;
	}
	/**
	 *  蛇的移动方法
	 *  去尾巴加头的方法
	 */
	public void move() {
		//去尾
		weiba=body.removeLast();
		/**
		 * 加头,先获得蛇的头,再给旧的头加一个新的头
		 * 默认移动方向为向右
		 * 定义出所有方向并完成changeDirection()方法
		 */
		int x=body.getFirst().x;
		int y=body.getFirst().y;
		switch (direction) {
		case UP:
			y--;
			if(y<0){
				y=Global.HEIGHT-1;
			}
			break;
		case DOWN:
			y++;
			if(y>=Global.HEIGHT){
				y=0; 
			}
			break;
		case LEFT:
			x--;
			if(x<0){
				x=Global.WIDTH-1;
			}
			break;
		case RIGHT:
			x++;
			if(x>=Global.WIDTH){
				x=0;
			}
			break;
		}
		body.addFirst(new Point(x, y));
		System.out.println(x+","+y);
		System.out.println("蛇正在移动");
	}
	/**
	 *  蛇的吃食物方法
	 * @param food
	 */
	public void eatFood(Food food) {
		body.addLast(weiba);//加尾巴
		System.out.println("蛇正在吃食物");
	}

	/**
	 * 改变方向
	 */
	public void changeDirection(int direction) {
		this.direction=direction;
		System.out.println("蛇正在改变方向");
	}
	/**
	 * 呈现自己
	 */
	public void showMe(Graphics g) {
		System.out.println("蛇正在展示自己");
		g.setColor(Color.ORANGE);
		if(life){
			for(Point p:body){
				g.fill3DRect(p.x*Global.CELL_SIZE, p.y*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
			}
		}
		int x=body.getFirst().x;
		int y=body.getFirst().y;
		g.setColor(Color.yellow);
		g.fill3DRect(x*Global.CELL_SIZE, y*Global.CELL_SIZE, Global.CELL_SIZE, Global.CELL_SIZE, true);
		
	}
	public boolean isEatMyself(){
		return false;
	}
	public void  addSnakeListener(SnakeListener snakeListener) {
		if(snakeListener!=null){
			this.snakeListener=snakeListener;
		}
	}
	
	
	/*
	 * 获取蛇头
	 */
	public Point getHead() {
		return body.getFirst();
	}
	/**
	 * 启动子线程
	 */
	public void start(){
		SnakeDrive sd=new SnakeDrive();
		sd.start();
		
	}
	/**
	 * 创建子线程
	 * 重写run方法
	 * @author Administrator
	 *
	 */
	private class SnakeDrive extends Thread {
		@Override
		public void run(){
			while(true){
				move();
				snakeListener.snakeMoved(Snake.this);
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

















